<?php

namespace lib;

class HttpCurl
{
    // url
    protected $_url = '';
    protected $_headers = [];
    protected $_data = null;
    protected $_cookie = null;
    protected $_referer = null;
    protected $_user_agent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36";
    protected $_timeout = 10;
    protected $_ch;

    public function __construct()
    {
        $this->_ch = curl_init();
        return $this;
    }

    public function exec()
    {
        try{
            $output = curl_exec($this->_ch);
            curl_close($this->_ch);
            return $output;
        }catch (Exception $exception){
            return $exception->getMessage();
        }
    }

    public function setType($type = false)
    {
        curl_setopt($this->_ch,CURLOPT_POST, $type);
        return $this;
    }

    /**
     * @return false|resource
     */
    public function setHeader($headers)
    {
        $this->_headers = array_merge($this->_headers,$headers);
        return $this->_ch;
    }

    public function setData($data)
    {
        $this->_data = $data;

        $this->_headers[] = sprintf("Content-length: %s",strlen($this->_data));
        return $this;
    }
}